# -*- coding: utf-8 -*-

"""
@Time    : 11/13/17 10:38 AM
@Author  : wong
@E-Mail  : wmx3ng@gmail.com
@File    : run.py
@Software: PyCharm
@Description:
"""
from app.utils import log_config
from app.cron_task.cron_config import Config
from flask import Flask
from flask_apscheduler import APScheduler
from flask_sqlalchemy import SQLAlchemy

logger = log_config.get_logger(__file__)

app = Flask(__name__, static_url_path='')
app.config.from_object('config')

# 配置数据库地址
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://%s:%s@%s:%d/%s" % (
    app.config['DB_USER'], app.config['DB_PASSWD'], app.config['DB_HOST'], app.config['DB_PORT'], app.config['DB_NAME'])
# 该配置为True,则每次请求结束都会自动commit数据库的变动
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)

# need import all data model, else the create_all will not valid.
from app.db_entity.models import *

db.create_all()


@app.route("/recommendation/api/v1.0/video/<user_id>", methods=['GET'])
def hello(user_id):
    u = User()
    u.username = user_id
    u.password = 'abc'

    db.session.add(u)
    db.session.commit()
    logger.debug('user_id:%s', user_id)
    return "hello world"


if __name__ == '__main__':
    scheduler = APScheduler()
    app.config.from_object(Config())

    # it is also possible to enable the API directly
    # scheduler.api_enabled = True
    scheduler.init_app(app)
    scheduler.start()

    app.run(host=app.config['SERVICE_HOST'], port=app.config['SERVICE_PORT'], debug=False)
